import os, sys
import time
from datetime import datetime
from subprocess import Popen, PIPE

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

class Logger(object):
    """just a logger class for this app to use"""

    def __init__(self, base=None):
        if base is None:
            self.base = 'unlabeled_logger_instance'
        else:
            self.base = base
        self.logdir = '%s/log' % APPLICATION_INSTALL_DIR
        self.logfile = os.path.join(self.logdir, str(time.strftime('%Y%m%d', datetime.now().timetuple())) + '.' + self.base + '.log')


    def log(self, text):
        """append to log"""
        lf = open(self.logfile, 'a+')
        timestamp = str(time.strftime('%Y/%m/%d-%H:%M:%S', datetime.now().timetuple()))
        lf.write(timestamp + ' -- ' + text + os.linesep)
        lf.close()

